***Data Cleaning and Coding for House Analyses***

*Code up viable primary challengers using DIME data
clear
import delimited "dime_recipients_1979_2020.csv"
*drop duplicates
drop if bonicarid == "" | bonicarid == " david chiu" | bonicarid == " supporting richard trujillo for state senate"
drop if primvotepct == .
keep if seat == "federal:house"
g incumbent = icostatus == "I"
keep if party == "100" | party == "200"
g dem = party == "100"
destring cycle, replace
drop if district == ""
*drop cases that shouldn't be there
*Jeff Flake ran for Senate in 2012
drop if bonicarid == "cand820" & cycle == 2012
*retirement/resignation after incumbent won primary
drop if district == "OH17" & dem == 1 & cycle == 2002
drop if district == "IL17" & dem == 1 & cycle == 2006
*fix mistake in district number
replace district = "CA16" if cycle == 2020 & bonicarid == "cand924" 
egen dist_party_cycle = group(district dem cycle)
g incprimwinner = incumbent == 1 & pwinner == "W"
*keep primary races in which incumbent won
egen numincprimwinner = sum(incprimwinner), by(dist_party_cycle)
drop if numincprimwinner == 0
*call the incumbent primary winner the "incumbent" (even in the rare cases where their closest opponent was also an incumbent)
drop incumbent
rename incprimwinner incumbent
drop numincprimwinner
*keep top performing challenger in each primary
sort incumbent dist_party_cycle incumbent primvotepct
drop if dist_party_cycle == dist_party_cycle[_n+1] & incumbent == 0 & primvotepct < primvotepct[_n+1]
egen totalcands = count(primvotepct), by(dist_party_cycle)
drop if totalcands == 1
drop totalcands
keep incumbent dist_party_cycle district dem cycle icpsr2 totalreceipts recipientcfscore
rename icpsr2 icpsr 
rename recipientcfscore cf
g extremism = cf
replace extremism = extremism*-1 if dem == 1
drop cf
rename totalreceipts receipts
reshape wide icpsr extremism receipts, i(dist_party_cycle) j(incumbent)
drop icpsr0
rename icpsr1 icpsr
replace icpsr = subinstr(icpsr, "OH_c_", "", .)
g viablechallenger = receipts0 >= 50000
g extremechallenger = extremism0 > extremism1
drop extremism* receipts*
drop if viablechallenger == 0
rename cycle eyear
keep eyear icpsr viablechallenger extremechallenger
drop if substr(icpsr, 1, 1) == "H"
destring icpsr, replace
save "HouseViablePrimaryChallengers.dta", replace

*Member experience and birth years
clear
import delimited using "HSall_members.csv"
keep if chamber == "House"
drop chamber
duplicates report icpsr congress
sort icpsr congress
g firstterm = icpsr != icpsr[_n-1]
g experience = firstterm if firstterm == 1
replace experience = experience[_n-1] + 1 if icpsr == icpsr[_n-1]
keep icpsr congress experience born died
keep if congress >= 104 & congress <= 117
save "experience.dta", replace

*Purple districts
clear
use "Pres_CD_formerge.dta"
egen dist_cycle = group(dist cycle)
reshape long pres, i(dist_cycle) j(year)
drop if pres == .
egen meanpres = mean(pres), by(dist_cycle)
sort dist_cycle
drop if dist_cycle == dist_cycle[_n-1]
drop pres year
rename meanpres pres
egen pres25 = pctile(pres), p(25) by(cycle)
egen pres75 = pctile(pres), p(75) by(cycle)
g purple = pres > pres25 & pres < pres75
keep dist cycle purple pres
split dist, p(-)
drop dist
rename dist1 state
rename dist2 dist
destring dist, replace
save "purple.dta", replace

*Clausen issue classifications
clear
import delimited "Hall_rollcalls_issues.csv"
drop if congress == 103
keep congress rollnumber clausen
g agriculture = clausen == "Agriculture" if clausen != "NA"
g civilliberties = clausen == "Civil Liberties" if clausen != "NA"
g defense = clausen == "Foreign and Defense Policy" if clausen != "NA"
g government = clausen == "Government Management" if clausen != "NA"
g welfare = clausen == "Social Welfare" if clausen != "NA"
drop clausen
save "clausen.dta", replace

*Get general election dates, filing dates, and primary dates into standard format for merge
clear
use "GenElecDates.dta"
g congress = (year - 1788)/2
foreach i in year month day {
	tostring `i', replace
}
g gdate = year + "-" + month + "-" + day
g gday = date(gdate, "YMD")
drop year month day
save "gdates.dta", replace
clear
use "filingdates.dta"
drop if chamber == "S"
drop chamber
g eyear = ceil(year/2)*2
g odd = year != eyear
tab month odd
drop odd
foreach i in year month day {
	tostring `i', replace
}
g fdate = year + "-" + month + "-" + day
g fday = date(fdate, "YMD")
g congress = (eyear - 1788)/2
drop year month day eyear
save "fdates.dta", replace
clear
use "primarydates.dta"
drop if chamber == "S"
drop chamber
g congress = (year - 1788)/2
foreach i in year month day {
	tostring `i', replace
}
g pdate = year + "-" + month + "-" + day
g pday = date(pdate, "YMD")
drop year month day
merge 1:1 state congress using "fdates.dta"
tab congress _merge
drop _merge
save "pdates.dta", replace

*extract dates of roll-call votes, merge in issues and party priority, code up nonprocedural
clear 
import delimited using "HSall_rollcalls.csv"
keep if chamber == "House"
keep if congress >= 104 & congress <= 117
g prop_yea = yea_count/(yea_count + nay_count)
keep congress rollnumber date vote_question bill_number prop_yea
merge 1:1 congress rollnumber using "clausen.dta"
drop _merge
merge m:1 congress bill_number using "prioritybills.dta"
drop if _merge == 2
g priority = _merge == 3
drop _merge bill_number
replace priority = . if congress == 116
replace vote_question = lower(vote_question)
replace vote_question = trim(vote_question)
g nonprocedural = vote_question == "on agreeing to the amendment"
replace nonprocedural = 1 if vote_question == "on agreeing to the conference report"
replace nonprocedural = 1 if vote_question == "on agreeing to the resolution"
replace nonprocedural = 1 if strpos(vote_question, "suspend the rules and pass") > 0
replace nonprocedural = 1 if vote_question == "on passage"
drop vote_question
rename date rcdate
g rcday = date(rcdate, "YMD")
save "rollcalldates.dta", replace

*Analyze vote-level data, merge in election date information, merge in district or member-specific information
clear
import delimited using "HSall_votes.csv"
keep if chamber == "House"
keep if congress >= 104 & congress <= 117
keep congress rollnumber icpsr cast_code
drop if cast_code == 0
g yea = cast_code <= 3 
replace yea = . if cast_code == 7 | cast_code == 8 | cast_code == 9
drop cast_code
g abstain = yea == .
merge m:1 congress icpsr using "PrimaryChallenges_1996_2022.dta"
keep if _merge == 3
drop _merge
merge m:1 congress rollnumber using "rollcalldates.dta"
drop _merge
merge m:1 state congress using "pdates.dta"
keep if _merge == 3
drop _merge
g afterprimary = rcday > pday
g afterfiling = rcday > fday
replace afterfiling = . if fday == .
*drop votes after the general election
merge m:1 congress using "gdates.dta"
drop _merge
drop if rcday > gday
drop gdate gday
rename rollnumber bill
*look at how others from someone's party voted
egen billpartyid = group(congress dem bill)
egen votes = count(yea), by(billpartyid)
egen yeas = sum(yea), by(billpartyid)
g meanyea = (yeas - yea)/(votes - 1)
g withparty = yea if meanyea >= .5
replace withparty = 1 - yea if meanyea < .5
drop billpartyid votes yeas meanyea
merge m:1 eyear icpsr using "HouseViablePrimaryChallengers.dta"
drop if _merge == 2
replace viablechallenger = 0 if viablechallenger == . & eyear <= 2020
replace viablechallenger = . if contested == 0
replace extremechallenger = . if contested == 0
drop _merge
merge m:1 congress icpsr using "experience.dta"
tab cong _merge
drop if _merge == 2
drop _merge
g cycle = floor((eyear - 4)/10)*10 + 2
merge m:1 state dist cycle using "purple.dta"
tab eyear _merge
drop if _merge == 2
drop _merge
egen minfday = min(fday), by(eyear)
save "RollCallVotes.dta", replace

*Estimate CVP scores, code directionality of each bill
forvalues cong = 104/117 {
	clear
	use "RollCallVotes.dta"
	keep if congress == `cong'
	*make initial guess about direction of each bill using party
	g consyea = yea if dem == 0
	g libyea = yea if dem == 1
	egen meanconsyea = mean(consyea), by(bill)
	egen meanlibyea = mean(libyea), by(bill)
	g conservative = yea
	replace conservative = 1 - yea if meanlibyea > meanconsyea
	drop *consyea *libyea
	*estimate CVP scores for each member and check the correlation between CVP and conservative voting for each bill
	quietly {
		egen id = group(icpsr)
		sum id
		local nmembers = r(max)
		egen billid = group(bill)
		sum billid
		local nbills = r(max)
		scalar wrongsign = 1
		while wrongsign > 0 {
			capture drop cvp
			xi:areg conservative i.id, a(bill) 
			g cvp = 0 if id == 1
			forvalues i = 2/`nmembers' {
			replace cvp = _b[_Iid_`i'] if id == `i'
			}
			forvalues i = 1/`nbills' {
			scalar wrongsign = 0
			reg conservative cvp if billid == `i'
				if _b[cvp] < 0 {
				replace conservative = 1 - conservative if billid == `i'
				scalar wrongsign = wrongsign + 1
				}
			}
			noisily:disp wrongsign
		}
		drop cvp
		xi:areg conservative i.id if rcday < minfday, a(bill)
		g cvp = 0 if id == 1
		forvalues i = 2/`nmembers' {
				replace cvp = _b[_Iid_`i'] if id == `i'
				}
	}
	drop _Iid*
	save "BillCodings`cong'.dta", replace
	egen votes = count(yea), by(icpsr)
	sort icpsr
	drop if icpsr == icpsr[_n-1]
	sum cvp [fw = votes], d
	replace cvp = cvp - r(p50)
	keep icpsr dem cvp
	egen medcvp = median(cvp), by(dem)
	g moderate = 0
	replace moderate = 1 if cvp < medcvp & dem == 0
	replace moderate = 1 if cvp > medcvp & dem == 1
	keep icpsr cvp moderate
	save "cvp`cong'.dta", replace
	clear
	use "BillCodings`cong'.dta"
	drop cvp
	merge m:1 icpsr using "cvp`cong'.dta"
	drop _merge
	save "CleanedData`cong'.dta", replace
}
clear
use "CleanedData104.dta"
forvalues i = 105/117 {
	append using "CleanedData`i'.dta"
}
drop id billid 
*code variables necessary for subsequent analyses
g extreme = conservative
replace extreme = 1 - conservative if dem == 1
egen billid = group(congress bill)
egen bill_party = group(dem congress bill)
egen member_cong = group(congress icpsr)

drop otheroffice rcdate pdate fdate minfday cycle

save "CleanedData_House.dta", replace

*erase files that we generated but no longer need
forvalues i = 104/117 {
	foreach j in CleanedData cvp BillCodings {
	erase "`j'`i'.dta"
	}
}
erase "RollCallVotes.dta"
erase "rollcalldates.dta"
erase "purple.dta"
erase "pdates.dta"
erase "gdates.dta"
erase "fdates.dta"
erase "experience.dta"
erase "clausen.dta"
erase "HouseViablePrimaryChallengers.dta"



***Data Cleaning and Coding for Senate Analyses***

*find viable challengers using DIME data
clear
import delimited "dime_recipients_1979_2020.csv"
drop if bonicarid == "" | bonicarid == " david chiu" | bonicarid == " supporting richard trujillo for state senate"
drop if primvotepct == .
keep if seat == "federal:senate"
g incumbent = icostatus == "I"
keep if party == "100" | party == "200"
g dem = party == "100"
destring cycle, replace
drop if district == ""
*fix errors
replace incumbent = 0 if bonicarid == "cand766" & cycle == 2014
drop if district == "NYS10" & cycle == 2010 & dem == 1
drop if bonicarid == "cand105793" & cycle == 2014
drop if bonicarid == "cand1161" & cycle == 2014
drop if bonicarid == "cand1588" & cycle == 2008
egen dist_party_cycle = group(district dem cycle)
g incprimwinner = incumbent == 1 & pwinner == "W"
*keep primary races in which incumbent won
egen numincprimwinner = sum(incprimwinner), by(dist_party_cycle)
drop if numincprimwinner == 0
*call the incumbent primary winner the "incumbent" (even in the rare cases where their closest opponent was also an incumbent)
drop incumbent
rename incprimwinner incumbent
drop numincprimwinner
*keep top performing challenger in each primary
sort incumbent dist_party_cycle incumbent primvotepct
drop if dist_party_cycle == dist_party_cycle[_n+1] & incumbent == 0 & primvotepct < primvotepct[_n+1]
egen totalcands = count(primvotepct), by(dist_party_cycle)
drop if totalcands == 1
drop totalcands
keep incumbent dist_party_cycle district dem cycle icpsr2 totalreceipts recipientcfscore
rename icpsr2 icpsr 
rename recipientcfscore cf
g extremism = cf
replace extremism = extremism*-1 if dem == 1
drop cf
rename totalreceipts receipts
reshape wide icpsr extremism receipts, i(dist_party_cycle) j(incumbent)
drop icpsr0
rename icpsr1 icpsr
drop if substr(icpsr, 1, 1) == "S"
destring icpsr, replace
g viablechallenger = receipts0 >= 50000
g extremechallenger = extremism0 > extremism1
drop extremism* receipts*
drop if viablechallenger == 0
rename cycle eyear
keep eyear icpsr viablechallenger extremechallenger
save "SenateViablePrimaryChallengers.dta", replace

*member experience and birth years
clear
import delimited using "HSall_members.csv"
keep if chamber == "Senate"
drop chamber
duplicates report icpsr congress
sort icpsr congress
g firstterm = icpsr != icpsr[_n-1]
g experience = firstterm if firstterm == 1
replace experience = experience[_n-1] + 1 if icpsr == icpsr[_n-1]
keep icpsr congress experience born died
keep if congress >= 104 & congress <= 117
save "senateexperience.dta", replace


*code purple states
clear
use "Pres_State_1916_2020.dta"
drop if year < 1996
drop if state == "DC"
g pres = d/(d + r)
collapse pres, by(state)
egen pres25 = pctile(pres), p(25) 
egen pres75 = pctile(pres), p(75)
g purple = pres > pres25 & pres < pres75
keep state purple pres
save "purplestates.dta", replace

clear
import delimited using "HSall_members.csv"
keep if chamber == "Senate"
sort bioguide_id congress icpsr
egen firstcong = min(cong), by(bioguide_id)
g experience = cong - firstcong + 1
keep congress icpsr state_abbrev party_code bioname experience 
rename state_abbrev state 
drop if state == "AS" | state == "DC" | state == "GU" | state == "MP" | state == "PR" | state == "VI"
g eyear = 1788 + congress*2
*drop third-party members
drop if party_code != 100 & party_code != 200
g dem = party_code == 100
drop party_code
rename bioname name
keep if congress >= 104 & congress <= 117
*need to identify which members are up for reelection
*as a first pass, see if this is their 3rd, 6th, 9th, etc. Congress
g reelection = round(experience/3) == experience/3
order icpsr name state eyear reelection dem
sort icpsr eyear
*browse
*look at special election winners using https://en.wikipedia.org/wiki/List_of_special_elections_to_the_United_States_Senate
g class = .
replace class = 1 if icpsr == 20735 | icpsr == 40915 | icpsr == 40913 | icpsr == 40707 | icpsr == 29534 | icpsr == 29369 | icpsr == 49306 | icpsr == 15502 | icpsr == 49300 | icpsr == 14400 | icpsr == 10808 | icpsr == 49905 | icpsr == 29373 | icpsr == 20730
replace class = 2 if icpsr == 41707 | icpsr == 41706 | icpsr == 41705 | icpsr == 41308 | icpsr == 14435 | icpsr == 40916 | icpsr == 49503 | icpsr == 15424 | icpsr == 14108 | icpsr == 12109
replace class = 3 if icpsr == 42104 | icpsr == 42105 | icpsr == 41905 | icpsr == 21173 | icpsr == 41112 | icpsr == 21166 | icpsr == 20115 | icpsr == 49904 | icpsr == 29523 | icpsr == 14871 | icpsr == 14806 | icpsr == 11204 | icpsr == 94659 | icpsr == 40300 | icpsr == 40910 | icpsr == 41112 | icpsr == 21166 | icpsr == 21501 | icpsr == 41904
*Gorton, Lautenberg, and Kyl switched Senate classes
replace class = 1 if icpsr == 14904 & congress >= 101
replace class = 2 if icpsr == 14914 & congress >= 108
replace class = 3 if icpsr == 15429 & congress >= 115
replace reelection = 0 if class != .
replace reelection = 1 if round((eyear + (3 - class)*2)/6) == (eyear + (3 - class)*2)/6 & class != .
*special elections and partial terms
replace reelection = 1 if icpsr == 40102 & eyear == 2002
replace reelection = 1 if icpsr == 40901 & eyear == 2010
replace reelection = 1 if icpsr == 40903 & eyear == 2010
replace reelection = 1 if icpsr == 40912 & eyear == 2010
replace reelection = 1 if icpsr == 40914 & eyear == 2010
replace reelection = 1 if icpsr == 40911 & eyear == 2010
replace reelection = 1 if icpsr == 20735 & eyear == 2010
replace reelection = 1 if icpsr == 41112 & eyear == 2014
replace reelection = 1 if icpsr == 41309 & eyear == 2014
replace reelection = 1 if icpsr == 21173 & eyear == 2014
replace reelection = 1 if icpsr == 41706 & eyear == 2018
replace reelection = 1 if icpsr == 21501 & eyear == 2020
replace reelection = 1 if icpsr == 41904 & eyear == 2020
drop class 
merge m:1 state using "purplestates.dta"
drop _merge
merge m:1 icpsr congress using "senateexperience.dta"
drop if _merge == 2
drop _merge
merge m:1 icpsr eyear using "SenateViablePrimaryChallengers.dta"
drop if _merge == 2
drop _merge
sort icpsr congress
foreach j of varlist viablechallenger extremechallenger { 
	forvalues i = 1/2 {
	replace `j' = `j'[_n+`i'] if icpsr == icpsr[_n+`i'] & congress + `i' == congress[_n+`i'] & reelection == 0 & `j' == .
	}
}
replace viablechallenger = 0 if viablechallenger == . & eyear <= 2020
merge 1:1 icpsr eyear using "contested_senate.dta"
tab _merge reelection
drop _merge
*apply same values for the entire electoral term
sort icpsr congress 
foreach j in contested retiring {
	forvalues i = 1/2 {
	replace `j' = `j'[_n+`i'] if `j' == . & icpsr == icpsr[_n+`i'] & congress + `i' == congress[_n+`i']
	}
}
*remaining missing values are cases where a senator left before the end of their term, leave contested missing, set retiring to 1
replace retiring = 1 if retiring == .
*code senators who lost primary
*Joe Lieberman lost but CT is excluded
g lostprimary = 0
replace lostprimary = 1 if icpsr == 15116 & eyear <= 2002 & eyear >= 1998
replace lostprimary = 1 if icpsr == 49504 & eyear == 1996
replace lostprimary = 1 if icpsr == 94910 & eyear == 2010
replace lostprimary = 1 if icpsr == 49307 & eyear <= 2010 & eyear >= 2006
replace lostprimary = 1 if icpsr == 14506 & eyear <= 2012 & eyear >= 2008
replace viablechallenger = . if contested == 0 | contested == .
replace extremechallenger = . if viablechallenger == 0 | viablechallenger == .
save "members.dta", replace

*organize dates
clear
use "GenElecDates.dta"
g congress = (year - 1788)/2
foreach i in year month day {
	tostring `i', replace
}
g gdate = year + "-" + month + "-" + day
g gday = date(gdate, "YMD")
drop year month day
save "gdates.dta", replace
clear
use "filingdates.dta"
drop if chamber == "H"
drop chamber
g eyear = ceil(year/2)*2
g odd = year != eyear
tab month odd

drop odd
foreach i in year month day {
	tostring `i', replace
}
g fdate = year + "-" + month + "-" + day
g fday = date(fdate, "YMD")
g congress = (eyear - 1788)/2
drop year month day eyear
save "fdates.dta", replace
clear
use "primarydates.dta"
drop if chamber == "H"
drop chamber
g congress = (year - 1788)/2
foreach i in year month day {
	tostring `i', replace
}
g pdate = year + "-" + month + "-" + day
g pday = date(pdate, "YMD")
drop year month day
merge 1:1 state congress using "fdates.dta"
tab congress _merge
drop _merge
save "pdates.dta", replace

*extract roll-call dates and code nonprocedural
clear 
import delimited using "HSall_rollcalls.csv"
keep if chamber == "Senate"
keep if congress >= 104 & congress <= 117
g prop_yea = yea_count/(yea_count + nay_count)
replace vote_question = lower(vote_question)
replace vote_question = trim(vote_question)
g nonprocedural = vote_question == "on passage of the bill"
replace nonprocedural = 1 if vote_question == "guilty or not guilty"
replace nonprocedural = 1 if vote_question == "on overriding the veto"
replace nonprocedural = 1 if vote_question == "on the amendment"
replace nonprocedural = 1 if vote_question == "on the conference report"
replace nonprocedural = 1 if vote_question == "on the joint resolution"
replace nonprocedural = 1 if vote_question == "on the nomination"
replace nonprocedural = 1 if vote_question == "on the resolution"
replace nonprocedural = 1 if vote_question == "on the resolution of ratification"
keep congress rollnumber date vote_question bill_number prop_yea nonprocedural
rename date rcdate
g rcday = date(rcdate, "YMD")
merge m:1 congress bill_number using "prioritybills.dta"
drop if _merge == 2
g priority = _merge == 3
replace priority = . if congress > 115
drop _merge
save "rollcalldates.dta", replace

*construct vote-level data set, merge in dates, member characteristics, bill characteristics
clear
import delimited using "HSall_votes.csv"
keep if chamber == "Senate"
keep if congress >= 104 & congress <= 117
keep congress rollnumber icpsr cast_code
drop if cast_code == 0
g yea = cast_code <= 3 
replace yea = . if cast_code == 7 | cast_code == 8 | cast_code == 9
drop cast_code
g abstain = yea == .
merge m:1 icpsr congress using "members.dta"
keep if _merge == 3
drop _merge
merge m:1 congress rollnumber using "rollcalldates.dta"
drop _merge
merge m:1 state congress using "pdates.dta"
keep if _merge == 3
drop _merge
g afterprimary = rcday > pday
g afterfiling = rcday > fday
replace afterfiling = . if fday == .
replace afterprimary = 0 if reelection == 0
replace afterfiling = 0 if reelection == 0
*drop votes after the general election
merge m:1 congress using "gdates.dta"
drop _merge
drop if rcday > gday
drop gdate gday
rename rollnumber bill
*look at how others from someone's party voted
egen billpartyid = group(congress dem bill)
egen votes = count(yea), by(billpartyid)
egen yeas = sum(yea), by(billpartyid)
g meanyea = (yeas - yea)/(votes - 1)
g withparty = yea if meanyea >= .5
replace withparty = 1 - yea if meanyea < .5
drop billpartyid votes yeas meanyea
egen minfday = min(fday), by(eyear)
save "RollCallVotes.dta", replace

*estimate CVP scores and record directionality of each bill
forvalues cong = 104/117 {
	clear
	use "RollCallVotes.dta"
	keep if congress == `cong'
	*make initial guess about direction of each bill using party
	g consyea = yea if dem == 0
	g libyea = yea if dem == 1
	egen meanconsyea = mean(consyea), by(bill)
	egen meanlibyea = mean(libyea), by(bill)
	g conservative = yea
	replace conservative = 1 - yea if meanlibyea > meanconsyea
	drop *consyea *libyea
	*estimate CVP scores for each member and check the correlation between CVP and conservative voting for each bill
	quietly {
		egen id = group(icpsr)
		sum id
		local nmembers = r(max)
		egen billid = group(bill)
		sum billid
		local nbills = r(max)
		scalar wrongsign = 1
		while wrongsign > 0 {
			capture drop cvp
			xi:areg conservative i.id, a(bill) 
			g cvp = 0 if id == 1
			forvalues i = 2/`nmembers' {
			replace cvp = _b[_Iid_`i'] if id == `i'
			}
			forvalues i = 1/`nbills' {
			scalar wrongsign = 0
			reg conservative cvp if billid == `i'
				if _b[cvp] < 0 {
				replace conservative = 1 - conservative if billid == `i'
				scalar wrongsign = wrongsign + 1
				}
			}
			noisily:disp wrongsign
		}
		drop cvp
		xi:areg conservative i.id if rcday < minfday, a(bill)
		g cvp = 0 if id == 1
		forvalues i = 2/`nmembers' {
				replace cvp = _b[_Iid_`i'] if id == `i'
				}
	}
	drop _Iid*
	save "BillCodings`cong'.dta", replace
	sort icpsr
	drop if icpsr == icpsr[_n-1]
	sum cvp, d
	replace cvp = cvp - r(p50)
	keep icpsr dem cvp
	egen medcvp = median(cvp), by(dem)
	g moderate = cvp < medcvp
	replace moderate = 1 - moderate if dem == 1
	keep icpsr cvp moderate
	save "cvp`cong'.dta", replace
	clear
	use "BillCodings`cong'.dta"
	merge m:1 icpsr using "cvp`cong'.dta"
	drop _merge
	save "CleanedData`cong'.dta", replace
}
clear
use "CleanedData104.dta"
forvalues i = 105/117 {
	append using "CleanedData`i'.dta"
}
drop id billid 
*additional variables necessary for analysis
g extreme = conservative
replace extreme = 1 - conservative if dem == 1
egen billid = group(congress bill)
egen bill_party = group(dem congress bill)
egen member_cong = group(congress icpsr)
drop if state == "CT"
drop rcdate pdate fdate minfday bill_number vote_question
save "CleanedData_Senate.dta", replace
*erase files that we generated but no longer need
forvalues i = 104/117 {
	foreach j in CleanedData cvp BillCodings {
	erase "`j'`i'.dta"
	}
}
erase "RollCallVotes.dta"
erase "rollcalldates.dta"
erase "fdates.dta"
erase "gdates.dta"
erase "members.dta"
erase "purplestates.dta"
erase "senateexperience.dta"
erase "SenateViablePrimaryChallengers.dta"


***Replication Code for Tables and Figures using the "CleanedData" files that we just generated***

***Table 1***
*House
clear
use "CleanedData_House.dta"
drop if state == "CT"
keep extreme withparty conservative afterprimary member_cong bill_party icpsr billid dem
areg extreme afterprimary, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using Table1, replace dec(4) alpha(.01, .05) symbol(**, *)
areg withparty afterprimary, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using Table1, append dec(4) alpha(.01, .05) symbol(**, *)
*average difference between Democrats and Republicans, discussed in Footnote 5
g rep = 1 - dem
areg conservative rep, a(billid)
*baseline levels of extreme and partisan voting, discussed in "Main Results" section
sum extreme withparty if afterprimary == 0
*Senate
clear
use "CleanedData_Senate.dta"
keep extreme withparty afterprimary member_cong bill_party icpsr billid
areg extreme afterprimary, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using Table1, append dec(4) alpha(.01, .05) symbol(**, *)
areg withparty afterprimary, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using Table1, append dec(4) alpha(.01, .05) symbol(**, *)


***Figure 1***
*House
clear
postutil clear
use "CleanedData_House.dta"
drop if state == "CT"
g diff = rcday - pday
sum diff
g months = floor(diff/30)
keep if abs(months) <= 4
g pre4 = months == -4
g pre3 = months == -3
g pre2 = months == -2
g post0 = months == 0
g post1 = months == 1
g post2 = months == 2
g post3 = months == 3
g post4 = months == 4
keep extreme pre4 pre3 pre2 post0 post1 post2 post3 post4 member_cong bill_party icpsr billid
areg extreme pre4 pre3 pre2 post0 post1 post2 post3 post4, a(member_cong bill_party) vce(cluster icpsr billid)
postfile EventStudy coef upper lower month using "EventStudy.dta", replace
forvalues i = 2/4 {
post EventStudy (_b[pre`i']) (_b[pre`i'] + _se[pre`i']*invttail(e(df_r),.025)) (_b[pre`i'] - _se[pre`i']*invttail(e(df_r),.025)) (`i'*-1) 
}
post EventStudy (0) (0) (0) (-1) 
forvalues i = 0/4 {
post EventStudy (_b[post`i']) (_b[post`i'] + _se[post`i']*invttail(e(df_r),.025)) (_b[post`i'] - _se[post`i']*invttail(e(df_r),.025)) (`i') 
}
postclose EventStudy
clear
use "EventStudy.dta"
sort month
graph twoway line upper lower coef month, yline(0)
gr_edit .plotregion1._xylines[1].style.editstyle linestyle(color(gs8) width(medthin)) editcopy
gr_edit .legend.draw_view.setstyle, style(no)
gr_edit .xaxis1.title.text = {}
gr_edit .xaxis1.title.text.Arrpush Months after Primary
gr_edit .yaxis1.title.text = {}
gr_edit .yaxis1.title.text.Arrpush Effect on Extreme Voting
gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .xaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
gr_edit .plotregion1.plot1.style.editstyle line(color(black) width(medthin) pattern(dash)) editcopy
gr_edit .plotregion1.plot2.style.editstyle line(color(black) width(medthin) pattern(dash)) editcopy
gr_edit .plotregion1.plot3.style.editstyle line(color(black) width(thick)) editcopy
gr_edit .plotregion1._xylines[1].style.editstyle linestyle(pattern(solid)) editcopy
graph save "EventStudy_House.gph", replace
*Senate
clear
use "CleanedData_Senate.dta"
g diff = rcday - pday
sum diff
g months = floor(diff/30)
keep if abs(months) <= 4
g pre4 = months == -4
g pre3 = months == -3
g pre2 = months == -2
g post0 = months == 0
g post1 = months == 1
g post2 = months == 2
g post3 = months == 3
g post4 = months == 4
keep extreme pre4 pre3 pre2 post0 post1 post2 post3 post4 member_cong bill_party icpsr billid
areg extreme pre4 pre3 pre2 post0 post1 post2 post3 post4, a(member_cong bill_party) vce(cluster icpsr billid)
postfile EventStudy coef upper lower month using "EventStudy.dta", replace
forvalues i = 2/4 {
post EventStudy (_b[pre`i']) (_b[pre`i'] + _se[pre`i']*invttail(e(df_r),.025)) (_b[pre`i'] - _se[pre`i']*invttail(e(df_r),.025)) (`i'*-1) 
}
post EventStudy (0) (0) (0) (-1) 
forvalues i = 0/4 {
post EventStudy (_b[post`i']) (_b[post`i'] + _se[post`i']*invttail(e(df_r),.025)) (_b[post`i'] - _se[post`i']*invttail(e(df_r),.025)) (`i') 
}
postclose EventStudy
clear
use "EventStudy.dta"
sort month
graph twoway line upper lower coef month, yline(0)
gr_edit .plotregion1._xylines[1].style.editstyle linestyle(color(gs8) width(medthin)) editcopy
gr_edit .legend.draw_view.setstyle, style(no)
gr_edit .xaxis1.title.text = {}
gr_edit .xaxis1.title.text.Arrpush Months after Primary
gr_edit .yaxis1.title.text = {}
gr_edit .yaxis1.title.text.Arrpush Effect on Extreme Voting
gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .xaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
gr_edit .plotregion1.plot1.style.editstyle line(color(black) width(medthin) pattern(dash)) editcopy
gr_edit .plotregion1.plot2.style.editstyle line(color(black) width(medthin) pattern(dash)) editcopy
gr_edit .plotregion1.plot3.style.editstyle line(color(black) width(thick)) editcopy
gr_edit .yaxis1.reset_rule -.03 .02 .01 , tickset(major) ruletype(range) 
gr_edit .plotregion1._xylines[1].style.editstyle linestyle(pattern(solid)) editcopy
graph save "EventStudy_Senate.gph", replace
*Combined
graph combine "EventStudy_House.gph" "EventStudy_Senate.gph", col(1)
gr_edit .style.editstyle margin(zero) boxstyle(shadestyle(color(white)) linestyle(color(white))) declared_ysize(6) editcopy
gr_edit .plotregion1.graph1.xaxis1.title.draw_view.setstyle, style(no)
gr_edit .plotregion1.graph1.title.style.editstyle color(black) size(medlarge) editcopy
gr_edit .plotregion1.graph1.title.text = {}
gr_edit .plotregion1.graph1.title.text.Arrpush U.S. House
gr_edit .plotregion1.graph1.title.style.editstyle  editcopy
gr_edit .plotregion1.graph2.title.style.editstyle color(black) size(medlarge) editcopy
gr_edit .plotregion1.graph2.title.text = {}
gr_edit .plotregion1.graph2.title.text.Arrpush U.S. Senate
gr_edit .style.editstyle declared_ysize(6) editcopy
gr_edit .style.editstyle declared_xsize(5.5) editcopy
graph export "Figure1.png", replace as(png)


***Table 2***
*House
clear
use "CleanedData_House.dta"
drop if state == "CT"
g primaryseason = afterfiling == 1 & afterprimary == 0
g primaryseason_moderate = primaryseason*moderate
keep abstain primaryseason primaryseason_moderate member_cong bill_party icpsr billid
areg abstain primaryseason, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using Table2, replace dec(4) alpha(.01, .05) symbol(**, *)
areg abstain primaryseason primaryseason_moderate, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using Table2, append dec(4) alpha(.01, .05) symbol(**, *)
*Senate
clear
use "CleanedData_Senate.dta"
g primaryseason = afterfiling == 1 & afterprimary == 0
g primaryseason_moderate = primaryseason*moderate
keep abstain primaryseason primaryseason_moderate member_cong bill_party icpsr billid
areg abstain primaryseason, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using Table2, append dec(4) alpha(.01, .05) symbol(**, *)
areg abstain primaryseason primaryseason_moderate, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using Table2, append dec(4) alpha(.01, .05) symbol(**, *)


***Figure 2***
clear
postutil clear
use "CleanedData_House.dta"
drop if state == "CT"
keep extreme afterprimary eyear member_cong bill_party icpsr billid
postfile Results coef upper lower eyear using "Results.dta", replace
forvalues i = 1998(2)2020 {
areg extreme afterprimary if abs(eyear - `i') < 3, a(member_cong bill_party) vce(cluster icpsr billid)
post Results (_b[afterprimary]) (_b[afterprimary] + _se[afterprimary]*invttail(e(df_r),.025)) (_b[afterprimary] - _se[afterprimary]*invttail(e(df_r),.025)) (`i') 
}
postclose Results
clear
use "Results.dta"
graph twoway line upper lower coef eyear, yline(0)
gr_edit .plotregion1._xylines[1].style.editstyle linestyle(color(gs8) width(medthin)) editcopy
gr_edit .legend.draw_view.setstyle, style(no)
gr_edit .xaxis1.title.text = {}
gr_edit .xaxis1.title.text.Arrpush Election Year
gr_edit .yaxis1.title.text = {}
gr_edit .yaxis1.title.text.Arrpush Effect of Primary Date
gr_edit .xaxis1.reset_rule 2000 2020 4 , tickset(major) ruletype(range)  
gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .xaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .plotregion1._xylines[1].style.editstyle linestyle(pattern(solid)) editcopy
gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
gr_edit .plotregion1.plot1.style.editstyle line(color(black) width(medthin) pattern(dash)) editcopy
gr_edit .plotregion1.plot2.style.editstyle line(color(black) width(medthin) pattern(dash)) editcopy
gr_edit .plotregion1.plot3.style.editstyle line(color(black) width(thick)) editcopy
gr_edit .xaxis1.plotregion.xscale.curmax = 2020.5
graph save "TimeHouse.gph", replace
*Senate
clear
use "CleanedData_Senate.dta"
keep extreme afterprimary eyear member_cong bill_party icpsr billid
postfile Results coef upper lower eyear using "Results.dta", replace
forvalues i = 1998(2)2020 {
areg extreme afterprimary if abs(eyear - `i') < 3, a(member_cong bill_party) vce(cluster icpsr billid)
post Results (_b[afterprimary]) (_b[afterprimary] + _se[afterprimary]*invttail(e(df_r),.025)) (_b[afterprimary] - _se[afterprimary]*invttail(e(df_r),.025)) (`i') 
}
postclose Results
clear
use "Results.dta"
graph twoway line upper lower coef eyear, yline(0)
gr_edit .plotregion1._xylines[1].style.editstyle linestyle(color(gs8) width(medthin)) editcopy
gr_edit .legend.draw_view.setstyle, style(no)
gr_edit .xaxis1.title.text = {}
gr_edit .xaxis1.title.text.Arrpush Election Year
gr_edit .yaxis1.title.text = {}
gr_edit .yaxis1.title.text.Arrpush Effect of Primary Date
gr_edit .xaxis1.reset_rule 2000 2020 4 , tickset(major) ruletype(range) 
gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .xaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .plotregion1._xylines[1].style.editstyle linestyle(pattern(solid)) editcopy
gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
gr_edit .plotregion1.plot1.style.editstyle line(color(black) width(medthin) pattern(dash)) editcopy
gr_edit .plotregion1.plot2.style.editstyle line(color(black) width(medthin) pattern(dash)) editcopy
gr_edit .plotregion1.plot3.style.editstyle line(color(black) width(thick)) editcopy
gr_edit .xaxis1.plotregion.xscale.curmax = 2020.5
graph save "TimeSenate.gph", replace
*Combined
graph combine "TimeHouse.gph" "TimeSenate.gph", col(1)
gr_edit .style.editstyle margin(zero) boxstyle(shadestyle(color(white)) linestyle(color(white))) declared_ysize(6) editcopy
gr_edit .plotregion1.graph1.xaxis1.title.draw_view.setstyle, style(no)
gr_edit .plotregion1.graph1.title.style.editstyle color(black) size(medlarge) editcopy
gr_edit .plotregion1.graph1.title.text = {}
gr_edit .plotregion1.graph1.title.text.Arrpush U.S. House
gr_edit .plotregion1.graph1.title.style.editstyle  editcopy
gr_edit .plotregion1.graph2.title.style.editstyle color(black) size(medlarge) editcopy
gr_edit .plotregion1.graph2.title.text = {}
gr_edit .plotregion1.graph2.title.text.Arrpush U.S. Senate
gr_edit .style.editstyle declared_ysize(6) editcopy
gr_edit .style.editstyle declared_xsize(5.5) editcopy
graph export "Figure2.png", replace as(png)


***Table 3***
*House
clear
postutil clear
use "CleanedData_House.dta"
drop if state == "CT"
g experienced = experience >= 5
g toptwo = state == "WA" & eyear >= 2008
replace toptwo = 1 if state == "CA" & eyear >= 2012
replace toptwo = 1 if state == "AK" & eyear >= 2022
g closed = state == "DE" | state == "FL" | state == "KY" | state == "MD" | state == "NV" | state == "NM" | state == "NY" | state == "OR" | state == "PA" 
g majority = dem
replace majority = 1 - dem if cong >= 104 & cong <= 109
replace majority = 1 - dem if cong >= 112 & cong <= 115
g closevote = prop_yea >= .35 & prop_yea < .65
g extremist = moderate == 0
g republican = dem == 0
g procedural = nonprocedural == 0
replace contested = . if retiring == 1
keep extreme afterprimary member_cong bill_party icpsr billid contested retiring purple viablechallenger extremechallenger closed toptwo moderate republican majority experienced closevote priority procedural
postfile Heterogeneity coef sterr pval str25 row str4 condition using "Heterogeneity.dta", replace
foreach i in contested retiring purple viablechallenger extremechallenger closed toptwo moderate republican majority experienced closevote priority procedural {
g afterprimary_`i' = afterprimary*`i'
egen bill_party_`i' = group(bill_party `i')
egen member_cong_`i' = group(member_cong `i')
areg extreme afterprimary if `i' == 1, a(member_cong bill_party) vce(cluster icpsr billid)
post Heterogeneity (_b[afterprimary]) (_se[afterprimary]) (2*ttail(e(df_r),abs(_b[afterprimary]/_se[afterprimary]))) ("`i'") ("yes") 
areg extreme afterprimary if `i' == 0, a(member_cong bill_party) vce(cluster icpsr billid)
post Heterogeneity (_b[afterprimary]) (_se[afterprimary]) (2*ttail(e(df_r),abs(_b[afterprimary]/_se[afterprimary]))) ("`i'") ("no") 
areg extreme afterprimary afterprimary_`i', a(member_cong_`i' bill_party_`i') vce(cluster icpsr billid)
post Heterogeneity (_b[afterprimary_`i']) (_se[afterprimary_`i']) (2*ttail(e(df_r),abs(_b[afterprimary_`i']/_se[afterprimary_`i']))) ("`i'") ("diff") 
drop afterprimary_`i' bill_party_`i' member_cong_`i'
}
postclose Heterogeneity
clear 
use "Heterogeneity.dta"
g rowno = _n
replace rowno = rowno[_n-1] if row == row[_n-1]
tostring coef, replace format(%5.4f) force
replace coef = subinstr(coef, "0.", ".", .)
tostring sterr, replace format(%5.4f) force
replace sterr = subinstr(sterr, "0.", ".", .)
g sig = "*" if pval < .05
replace sig = "**" if pval < .01
replace coef = coef + sig
replace sterr = "(" + sterr + ")"
keep row rowno condition coef sterr
reshape wide coef sterr, i(rowno) j(condition) string
foreach i in no yes diff {
foreach j in coef sterr {
rename `j'`i' `i'`j'
}
}
reshape long no yes diff, i(rowno) j(q) string
order row no yes diff
replace row = "" if row == row[_n-1]
drop rowno q
save "Table3_House.dta", replace
*Senate
clear
use "CleanedData_Senate.dta"
g experienced = experience >= 5
g toptwo = state == "WA" & eyear >= 2008
replace toptwo = 1 if state == "CA" & eyear >= 2012
replace toptwo = 1 if state == "AK" & eyear >= 2022
g closed = state == "DE" | state == "FL" | state == "KY" | state == "MD" | state == "NV" | state == "NM" | state == "NY" | state == "OR" | state == "PA" 
*party control flipped in the 107th, but Dems had it for most of the time, so we code it as Dem control
g majority = dem
replace majority = 1 - dem if cong >= 104 & cong <= 106
replace majority = 1 - dem if cong >= 108 & cong <= 109
replace majority = 1 - dem if cong >= 114 & cong <= 116
g closevote = prop_yea >= .35 & prop_yea < .65
g extremist = moderate == 0
g republican = dem == 0
g procedural = nonprocedural == 0
replace contested = . if retiring == 1
keep extreme afterprimary member_cong bill_party icpsr billid contested retiring purple viablechallenger extremechallenger closed toptwo moderate republican majority experienced closevote priority procedural
postfile Heterogeneity coef sterr pval str25 row str4 condition using "Heterogeneity.dta", replace
foreach i in contested retiring purple viablechallenger extremechallenger closed toptwo moderate republican majority experienced closevote priority procedural {
g afterprimary_`i' = afterprimary*`i'
egen bill_party_`i' = group(bill_party `i')
egen member_cong_`i' = group(member_cong `i')
areg extreme afterprimary if `i' == 1, a(member_cong bill_party) vce(cluster icpsr billid)
post Heterogeneity (_b[afterprimary]) (_se[afterprimary]) (2*ttail(e(df_r),abs(_b[afterprimary]/_se[afterprimary]))) ("`i'") ("yes") 
areg extreme afterprimary if `i' == 0, a(member_cong bill_party) vce(cluster icpsr billid)
post Heterogeneity (_b[afterprimary]) (_se[afterprimary]) (2*ttail(e(df_r),abs(_b[afterprimary]/_se[afterprimary]))) ("`i'") ("no") 
areg extreme afterprimary afterprimary_`i', a(member_cong_`i' bill_party_`i') vce(cluster icpsr billid)
post Heterogeneity (_b[afterprimary_`i']) (_se[afterprimary_`i']) (2*ttail(e(df_r),abs(_b[afterprimary_`i']/_se[afterprimary_`i']))) ("`i'") ("diff") 
drop afterprimary_`i' bill_party_`i' member_cong_`i'
}
postclose Heterogeneity
clear 
use "Heterogeneity.dta"
g rowno = _n
replace rowno = rowno[_n-1] if row == row[_n-1]
tostring coef, replace format(%5.4f) force
replace coef = subinstr(coef, "0.", ".", .)
tostring sterr, replace format(%5.4f) force
replace sterr = subinstr(sterr, "0.", ".", .)
g sig = "*" if pval < .05
replace sig = "**" if pval < .01
replace coef = coef + sig
replace sterr = "(" + sterr + ")"
keep row rowno condition coef sterr
reshape wide coef sterr, i(rowno) j(condition) string
foreach i in no yes diff {
foreach j in coef sterr {
rename `j'`i' `i'`j'
}
}
reshape long no yes diff, i(rowno) j(q) string
order row no yes diff
replace row = "" if row == row[_n-1]
drop rowno q
save "Table3_Senate.dta", replace


*Analysis of different issues (discussed in the "Additional Heterogeneity" section)
*House
clear
use "CleanedData_House.dta"
drop if state == "CT"
keep extreme afterprimary agriculture civilliberties defense government welfare member_cong bill_party icpsr billid
*miscellaneous policy
areg extreme afterprimary if agriculture + civilliberties + defense + government + welfare == 0, a(member_cong bill_party) vce(cluster icpsr billid)
*government management
areg extreme afterprimary if government == 1, a(member_cong bill_party) vce(cluster icpsr billid)
*foreign and defense policy
areg extreme afterprimary if defense == 1, a(member_cong bill_party) vce(cluster icpsr billid)
*social welfare
areg extreme afterprimary if welfare == 1, a(member_cong bill_party) vce(cluster icpsr billid)
*agriculture
areg extreme afterprimary if agriculture == 1, a(member_cong bill_party) vce(cluster icpsr billid)
*civil liberties
areg extreme afterprimary if civilliberties == 1, a(member_cong bill_party) vce(cluster icpsr billid)


***Figure A1***
*we're using the pdates.dta file that we created in the Senate cleaning
clear
use "pdates.dta"
keep if congress == 116
sort pday fday state
g order = _n
replace order = 50 - order
replace pday = (pday - 22222)/30
replace fday = (fday - 22222)/30
graph twoway (scatter order fday) (scatter order pday, ml(state))
gr_edit .legend.draw_view.setstyle, style(no)
gr_edit .xaxis1.title.text = {}
gr_edit .xaxis1.title.text.Arrpush Months Before General Election Date
gr_edit .xaxis1.reset_rule -12 0 1, tickset(major) ruletype(range) 
gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .xaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
gr_edit .xaxis1.plotregion.xscale.curmax = 0.3
gr_edit .yaxis1.draw_view.setstyle, style(no)
gr_edit .style.editstyle declared_ysize(10) editcopy
gr_edit .style.editstyle declared_xsize(9) editcopy
gr_edit .plotregion1.plot1.style.editstyle marker(fillcolor(black) linestyle(color(black))) editcopy
gr_edit .plotregion1.plot2.style.editstyle label(textstyle(color(black) size(vsmall))) editcopy
gr_edit .plotregion1.plot2.style.editstyle marker(fillcolor(black) linestyle(color(black))) editcopy
forvalues i = 1/13 {
	local counter1 = 14 - `i'
	local counter2 = `i' - 13
	local counter3 = (`i' - 13)*-1
	gr_edit .xaxis1.major.num_rule_ticks = `counter1'
	gr_edit .xaxis1.edit_tick 1 `counter2' `"`counter3'"', tickset(major)
}
forvalues i = 0/49 {
	sum fday if order == `i'
	local start = r(mean)
	sum pday if order == `i'
	local end = r(mean)
	local counter = `i' + 1
	gr_edit .plotregion1.AddLine added_lines editor `start' `i' `end' `i'
	gr_edit .plotregion1.added_lines_new = `counter'
	gr_edit .plotregion1.added_lines_rec = `counter'
	gr_edit .plotregion1.added_lines[`counter'].style.editstyle  linestyle( width( sztype(relative) val(.2) allow_pct(1)) color(black) pattern(solid) align(inside)) headstyle( symbol(circle) linestyle( width( sztype(relative) val(.2) allow_pct(1)) color(black) pattern(solid) align(inside)) fillcolor(black) size( sztype(relative) val(1.52778) allow_pct(1)) angle(stdarrow) symangle(zero) backsymbol(none) backline( width( sztype(relative) val(.2) allow_pct(1)) color(black) pattern(solid) align(inside)) backcolor(black) backsize( sztype(relative) val(0) allow_pct(1)) backangle(stdarrow) backsymangle(zero)) headpos(neither) editcopy
}
graph export "FigureA1.png", replace as(png)


***Table A1***
*House
clear
use "CleanedData_House.dta"
drop if state == "CT"
keep extreme withparty afterprimary afterfiling member_cong bill_party icpsr billid
areg extreme afterprimary afterfiling, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using TableA1, replace dec(4) alpha(.01, .05) symbol(**, *)
areg withparty afterprimary afterfiling, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using TableA1, append dec(4) alpha(.01, .05) symbol(**, *)
*Senate
clear
use "CleanedData_Senate.dta"
keep extreme withparty afterprimary afterfiling member_cong bill_party icpsr billid
areg extreme afterprimary afterfiling, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using TableA1, append dec(4) alpha(.01, .05) symbol(**, *)
areg withparty afterprimary afterfiling, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using TableA1, append dec(4) alpha(.01, .05) symbol(**, *)


***Table A2***
*House
clear
use "CleanedData_House.dta"
drop if state == "CT"
keep if retiring == 0
keep extreme withparty afterprimary afterfiling member_cong bill_party icpsr billid contested
areg extreme afterprimary afterfiling if contested == 1, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using TableA2_TopPanel, replace dec(4) alpha(.01, .05) symbol(**, *)
areg withparty afterprimary afterfiling if contested == 1, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using TableA2_TopPanel, append dec(4) alpha(.01, .05) symbol(**, *)
areg extreme afterprimary afterfiling if contested == 0, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using TableA2_BottomPanel, replace dec(4) alpha(.01, .05) symbol(**, *)
areg withparty afterprimary afterfiling if contested == 0, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using TableA2_BottomPanel, append dec(4) alpha(.01, .05) symbol(**, *)
*Senate
clear
use "CleanedData_Senate.dta"
keep if retiring == 0
keep extreme withparty afterprimary afterfiling member_cong bill_party icpsr billid contested
areg extreme afterprimary afterfiling if contested == 1, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using TableA2_TopPanel, append dec(4) alpha(.01, .05) symbol(**, *)
areg withparty afterprimary afterfiling if contested == 1, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using TableA2_TopPanel, append dec(4) alpha(.01, .05) symbol(**, *)
areg extreme afterprimary afterfiling if contested == 0, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using TableA2_BottomPanel, append dec(4) alpha(.01, .05) symbol(**, *)
areg withparty afterprimary afterfiling if contested == 0, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using TableA2_BottomPanel, append dec(4) alpha(.01, .05) symbol(**, *)


***Table A3***
*House
clear
use "CleanedData_House.dta"
drop if state == "CT"
drop if retiring == 1
drop if lostprimary == 1
g renominated = afterprimary
replace renominated = afterfiling if contested == 0
keep extreme withparty renominated member_cong bill_party icpsr billid
areg extreme renominated, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using TableA3, replace dec(4) alpha(.01, .05) symbol(**, *)
areg withparty renominated, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using TableA3, append dec(4) alpha(.01, .05) symbol(**, *)
*Senate
clear
use "CleanedData_Senate.dta"
drop if retiring == 1
drop if lostprimary == 1
g renominated = afterprimary
replace renominated = afterfiling if contested == 0
keep extreme withparty renominated member_cong bill_party icpsr billid
areg extreme renominated, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using TableA3, append dec(4) alpha(.01, .05) symbol(**, *)
areg withparty renominated, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using TableA3, append dec(4) alpha(.01, .05) symbol(**, *)


***Figure A2***
*House
clear
postutil clear
use "CleanedData_House.dta"
drop if state == "CT"
keep extreme afterprimary eyear member_cong bill_party icpsr billid
postfile Results coef upper lower eyear using "Results.dta", replace
forvalues i = 1996(2)2022 {
areg extreme afterprimary if eyear == `i', a(member_cong bill_party) vce(cluster icpsr billid)
post Results (_b[afterprimary]) (_b[afterprimary] + _se[afterprimary]*invttail(e(df_r),.025)) (_b[afterprimary] - _se[afterprimary]*invttail(e(df_r),.025)) (`i') 
}
postclose Results
clear
use "Results.dta"
graph twoway line upper lower coef eyear, yline(0)
gr_edit .plotregion1._xylines[1].style.editstyle linestyle(color(gs8) width(medthin)) editcopy
gr_edit .legend.draw_view.setstyle, style(no)
gr_edit .xaxis1.title.text = {}
gr_edit .xaxis1.title.text.Arrpush Election Year
gr_edit .yaxis1.title.text = {}
gr_edit .yaxis1.title.text.Arrpush Effect of Primary Date
gr_edit .xaxis1.reset_rule 1996 2020 4 , tickset(major) ruletype(range) 
gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .xaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .plotregion1._xylines[1].style.editstyle linestyle(pattern(solid)) editcopy
gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
gr_edit .plotregion1.plot1.style.editstyle line(color(black) width(medthin) pattern(dash)) editcopy
gr_edit .plotregion1.plot2.style.editstyle line(color(black) width(medthin) pattern(dash)) editcopy
gr_edit .plotregion1.plot3.style.editstyle line(color(black) width(thick)) editcopy
graph save "TimeHouseAppendix.gph", replace
*Senate
clear
use "CleanedData_Senate.dta"
keep extreme afterprimary eyear member_cong bill_party icpsr billid
postfile Results coef upper lower eyear using "Results.dta", replace
forvalues i = 1996(2)2022 {
areg extreme afterprimary if eyear == `i', a(member_cong bill_party) vce(cluster icpsr billid)
post Results (_b[afterprimary]) (_b[afterprimary] + _se[afterprimary]*invttail(e(df_r),.025)) (_b[afterprimary] - _se[afterprimary]*invttail(e(df_r),.025)) (`i') 
}
postclose Results
clear
use "Results.dta"
graph twoway line upper lower coef eyear, yline(0)
gr_edit .plotregion1._xylines[1].style.editstyle linestyle(color(gs8) width(medthin)) editcopy
gr_edit .legend.draw_view.setstyle, style(no)
gr_edit .xaxis1.title.text = {}
gr_edit .xaxis1.title.text.Arrpush Election Year
gr_edit .yaxis1.title.text = {}
gr_edit .yaxis1.title.text.Arrpush Effect of Primary Date
gr_edit .xaxis1.reset_rule 1996 2020 4 , tickset(major) ruletype(range) 
gr_edit .yaxis1.reset_rule -.06 .02 .02 , tickset(major) ruletype(range) 
gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .xaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .plotregion1._xylines[1].style.editstyle linestyle(pattern(solid)) editcopy
gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
gr_edit .plotregion1.plot1.style.editstyle line(color(black) width(medthin) pattern(dash)) editcopy
gr_edit .plotregion1.plot2.style.editstyle line(color(black) width(medthin) pattern(dash)) editcopy
gr_edit .plotregion1.plot3.style.editstyle line(color(black) width(thick)) editcopy
graph save "TimeSenateAppendix.gph", replace
*Combined
graph combine "TimeHouseAppendix.gph" "TimeSenateAppendix.gph", col(1)
gr_edit .style.editstyle margin(zero) boxstyle(shadestyle(color(white)) linestyle(color(white))) declared_ysize(6) editcopy
gr_edit .plotregion1.graph1.xaxis1.title.draw_view.setstyle, style(no)
gr_edit .plotregion1.graph1.title.style.editstyle color(black) size(medlarge) editcopy
gr_edit .plotregion1.graph1.title.text = {}
gr_edit .plotregion1.graph1.title.text.Arrpush U.S. House
gr_edit .plotregion1.graph1.title.style.editstyle  editcopy
gr_edit .plotregion1.graph2.title.style.editstyle color(black) size(medlarge) editcopy
gr_edit .plotregion1.graph2.title.text = {}
gr_edit .plotregion1.graph2.title.text.Arrpush U.S. Senate
gr_edit .style.editstyle declared_ysize(6) editcopy
gr_edit .style.editstyle declared_xsize(5.5) editcopy
graph export "FigureA2.png", replace as(png)


***Table A4***
clear
use "CleanedData_House.dta"
drop if eyear < 2000
g convdate = ""
replace convdate = "2000-7-22" if state == "CT" & eyear == 2000 & dem == 1
replace convdate = "2000-7-10" if state == "CT" & eyear == 2000 & dem == 0 & dist == 1
replace convdate = "2000-7-8" if state == "CT" & eyear == 2000 & dem == 0 & dist == 2
replace convdate = "2000-7-10" if state == "CT" & eyear == 2000 & dem == 0 & dist == 3
replace convdate = "2000-7-10" if state == "CT" & eyear == 2000 & dem == 0 & dist == 4
replace convdate = "2000-7-10" if state == "CT" & eyear == 2000 & dem == 0 & dist == 5
replace convdate = "2000-7-15" if state == "CT" & eyear == 2000 & dem == 0 & dist == 6
replace convdate = "2002-7-20" if state == "CT" & eyear == 2002
replace convdate = "2004-5-10" if state == "CT" & eyear == 2004 & dem == 1
replace convdate = "2004-5-24" if state == "CT" & eyear == 2004 & dem == 0 & dist == 1
replace convdate = "2004-5-15" if state == "CT" & eyear == 2004 & dem == 0 & dist == 2
replace convdate = "2004-5-15" if state == "CT" & eyear == 2004 & dem == 0 & dist == 3
replace convdate = "2004-5-10" if state == "CT" & eyear == 2004 & dem == 0 & dist == 4
replace convdate = "2004-5-22" if state == "CT" & eyear == 2004 & dem == 0 & dist == 5
replace convdate = "2006-5-15" if state == "CT" & eyear == 2006 & dem == 1
replace convdate = "2006-5-10" if state == "CT" & eyear == 2006 & dem == 0 & dist == 1
replace convdate = "2006-5-21" if state == "CT" & eyear == 2006 & dem == 0 & dist == 2
replace convdate = "2006-5-6" if state == "CT" & eyear == 2006 & dem == 0 & dist == 3
replace convdate = "2006-5-15" if state == "CT" & eyear == 2006 & dem == 0 & dist == 4
replace convdate = "2006-5-6" if state == "CT" & eyear == 2006 & dem == 0 & dist == 5
replace convdate = "2008-5-12" if state == "CT" & eyear == 2008 & dem == 1
replace convdate = "2008-5-10" if state == "CT" & eyear == 2008 & dem == 0
replace convdate = "2010-5-10" if state == "CT" & eyear == 2010 & dem == 1
replace convdate = "2010-5-21" if state == "CT" & eyear == 2010 & dem == 0
replace convdate = "2012-5-14" if state == "CT" & eyear == 2012 & dem == 1
replace convdate = "2012-5-18" if state == "CT" & eyear == 2012 & dem == 0
replace convdate = "2014-5-14" if state == "CT" & eyear == 2014 & dem == 1
replace convdate = "2014-5-16" if state == "CT" & eyear == 2014 & dem == 0
replace convdate = "2016-5-9" if state == "CT" & eyear == 2016
replace convdate = "2018-5-14" if state == "CT" & eyear == 2018 & dem == 1
replace convdate = "2018-5-11" if state == "CT" & eyear == 2018 & dem == 0
replace convdate = "2020-5-11" if state == "CT" & eyear == 2020 & dem == 1
replace convdate = "2020-5-7" if state == "CT" & eyear == 2020 & dem == 0 & dist == 1
replace convdate = "2020-5-11" if state == "CT" & eyear == 2020 & dem == 0 & dist == 2
replace convdate = "2020-5-12" if state == "CT" & eyear == 2020 & dem == 0 & dist == 3
replace convdate = "2020-5-13" if state == "CT" & eyear == 2020 & dem == 0 & dist == 4
replace convdate = "2020-5-14" if state == "CT" & eyear == 2020 & dem == 0 & dist == 5
replace convdate = "2022-5-9" if state == "CT" & eyear == 2022 & dem == 1
replace convdate = "2022-5-5" if state == "CT" & eyear == 2022 & dem == 0
g cday = date(convdate, "YMD")
g afterconvention = rcday > cday
replace afterfiling = 0 if state == "CT"
replace afterprimary = 0 if state == "CT"
keep extreme withparty afterprimary afterfiling afterconvention member_cong bill_party icpsr billid
areg extreme afterprimary afterfiling afterconvention, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using TableA4, replace dec(4) alpha(.01, .05) symbol(**, *)
areg withparty afterprimary afterfiling afterconvention, a(member_cong bill_party) vce(cluster icpsr billid)
outreg2 using TableA4, append dec(4) alpha(.01, .05) symbol(**, *)


***Table A5***
**Panel 1**
*Generate data set for House
clear
local maxnpdays = 0
forvalues k = 104/117 {
quietly {
clear
use "CleanedData_House.dta"
drop if state == "CT"
keep cong billid icpsr dem extreme withparty afterprimary pday rcday
keep if cong == `k'
egen pdayid = group(pday)
sum pdayid
local npdays = r(max) - 1
local maxnpdays = max(`npdays', `maxnpdays')
forvalues i = 1/`npdays' {
preserve
sum pday if pdayid == `i'
keep if rcday <= r(mean) + 30
egen meanafterprimary = mean(afterprimary), by(icpsr)
keep if pdayid == `i' | meanafterprimary == 0
keep icpsr billid dem extreme withparty afterprimary
g dataset = `i'
g cong = `k' 
save "dataset_`i'.dta", replace
restore
}
clear
use "dataset_1.dta"
forvalues i = 2/`npdays' {
append using "dataset_`i'.dta"
}
save "dataset_cong_`k'.dta", replace
}
disp `k'
}
clear
use "dataset_cong_104.dta"
forvalues k = 105/117 {
append using "dataset_cong_`k'.dta"
}
save "House_Stacked.dta", replace
forvalues k = 104/117 {
erase "dataset_cong_`k'.dta"
}
forvalues i = 1/`maxnpdays' {
erase "dataset_`i'.dta"
}
*Generate data set for Senate*
clear
local maxnpdays = 0
forvalues k = 104/117 {
quietly {
clear
use "CleanedData_Senate.dta"
keep cong billid icpsr dem extreme withparty afterprimary pday rcday
keep if cong == `k'
egen pdayid = group(pday)
sum pdayid
local npdays = r(max) - 1
local maxnpdays = max(`npdays', `maxnpdays')
forvalues i = 1/`npdays' {
preserve
sum pday if pdayid == `i'
keep if rcday <= r(mean) + 30
egen meanafterprimary = mean(afterprimary), by(icpsr)
keep if pdayid == `i' | meanafterprimary == 0
keep icpsr billid dem extreme withparty afterprimary
g dataset = `i'
g cong = `k' 
save "dataset_`i'.dta", replace
restore
}
clear
use "dataset_1.dta"
forvalues i = 2/`npdays' {
append using "dataset_`i'.dta"
}
save "dataset_cong_`k'.dta", replace
}
disp `k'
}
clear
use "dataset_cong_104.dta"
forvalues k = 105/117 {
append using "dataset_cong_`k'.dta"
}
save "Senate_Stacked.dta", replace
forvalues k = 104/117 {
erase "dataset_cong_`k'.dta"
}
forvalues i = 1/`maxnpdays' {
erase "dataset_`i'.dta"
}
*Regressions
clear
use "House_Stacked.dta"
egen member_cong_dataset = group(icpsr dem cong dataset)
egen bill_party_dataset = group(cong billid dem dataset)
keep extreme withparty afterprimary member_cong_dataset bill_party_dataset icpsr billid
areg extreme afterprimary, a(member_cong_dataset bill_party_dataset) vce(cluster icpsr billid)
outreg2 using TableA5_Panel1, replace dec(4) alpha(.01, .05) symbol(**, *)
areg withparty afterprimary, a(member_cong_dataset bill_party_dataset) vce(cluster icpsr billid)
outreg2 using TableA5_Panel1, append dec(4) alpha(.01, .05) symbol(**, *)
clear
use "Senate_Stacked.dta"
egen member_cong_dataset = group(icpsr dem cong dataset)
egen bill_party_dataset = group(cong billid dem dataset)
keep extreme withparty afterprimary member_cong_dataset bill_party_dataset icpsr billid
areg extreme afterprimary, a(member_cong_dataset bill_party_dataset) vce(cluster icpsr billid)
outreg2 using TableA5_Panel1, append dec(4) alpha(.01, .05) symbol(**, *)
areg withparty afterprimary, a(member_cong_dataset bill_party_dataset) vce(cluster icpsr billid)
outreg2 using TableA5_Panel1, append dec(4) alpha(.01, .05) symbol(**, *)

**Panel 2**
*Generate data set for House
clear
local maxnpdays = 0
forvalues k = 104/117 {
quietly {
clear
use "CleanedData_House.dta"
drop if state == "CT"
keep cong billid icpsr dem extreme withparty afterprimary pday rcday
keep if cong == `k'
egen pdayid = group(pday)
sum pdayid
local npdays = r(max) - 1
local maxnpdays = max(`npdays', `maxnpdays')
forvalues i = 2/`npdays' {
preserve
sum pday if pdayid == `i'
keep if rcday >= r(mean) - 30
egen meanafterprimary = mean(afterprimary), by(icpsr)
keep if pdayid == `i' | meanafterprimary == 1
keep icpsr billid dem extreme withparty afterprimary
g dataset = `i'
g cong = `k' 
save "dataset_`i'.dta", replace
restore
}
clear
use "dataset_2.dta"
forvalues i = 3/`npdays' {
append using "dataset_`i'.dta"
}
save "dataset_cong_`k'.dta", replace
}
disp `k'
}
clear
use "dataset_cong_104.dta"
forvalues k = 105/117 {
append using "dataset_cong_`k'.dta"
}
save "House_Stacked_v2.dta", replace
forvalues k = 104/117 {
erase "dataset_cong_`k'.dta"
}
forvalues i = 2/`maxnpdays' {
erase "dataset_`i'.dta"
}
*Generate data set for Senate
clear
local maxnpdays = 0
forvalues k = 104/117 {
quietly {
clear
use "CleanedData_Senate.dta"
keep cong billid icpsr dem extreme withparty afterprimary pday rcday
keep if cong == `k'
egen pdayid = group(pday)
sum pdayid
local npdays = r(max) - 1
local maxnpdays = max(`npdays', `maxnpdays')
forvalues i = 2/`npdays' {
preserve
sum pday if pdayid == `i'
keep if rcday >= r(mean) - 30
egen meanafterprimary = mean(afterprimary), by(icpsr)
keep if pdayid == `i' | meanafterprimary == 1
keep icpsr billid dem extreme withparty afterprimary
g dataset = `i'
g cong = `k' 
save "dataset_`i'.dta", replace
restore
}
clear
use "dataset_2.dta"
forvalues i = 3/`npdays' {
append using "dataset_`i'.dta"
}
save "dataset_cong_`k'.dta", replace
}
disp `k'
}
clear
use "dataset_cong_104.dta"
forvalues k = 105/117 {
append using "dataset_cong_`k'.dta"
}
save "Senate_Stacked_v2.dta", replace
forvalues k = 104/117 {
erase "dataset_cong_`k'.dta"
}
forvalues i = 2/`maxnpdays' {
erase "dataset_`i'.dta"
}
*Regressions
clear
use "House_Stacked_v2.dta"
egen member_cong_dataset = group(icpsr dem cong dataset)
egen bill_party_dataset = group(cong billid dem dataset)
keep extreme withparty afterprimary member_cong_dataset bill_party_dataset icpsr billid
areg extreme afterprimary, a(member_cong_dataset bill_party_dataset) vce(cluster icpsr billid)
outreg2 using TableA5_Panel2, replace dec(4) alpha(.01, .05) symbol(**, *)
areg withparty afterprimary, a(member_cong_dataset bill_party_dataset) vce(cluster icpsr billid)
outreg2 using TableA5_Panel2, append dec(4) alpha(.01, .05) symbol(**, *)
clear
use "Senate_Stacked_v2.dta"
egen member_cong_dataset = group(icpsr dem cong dataset)
egen bill_party_dataset = group(cong billid dem dataset)
keep extreme withparty afterprimary member_cong_dataset bill_party_dataset icpsr billid
areg extreme afterprimary, a(member_cong_dataset bill_party_dataset) vce(cluster icpsr billid)
outreg2 using TableA5_Panel2, append dec(4) alpha(.01, .05) symbol(**, *)
areg withparty afterprimary, a(member_cong_dataset bill_party_dataset) vce(cluster icpsr billid)
outreg2 using TableA5_Panel2, append dec(4) alpha(.01, .05) symbol(**, *)

**Panel 3**
*Generate data set for House
clear
local maxnpdays = 0
forvalues k = 104/117 {
quietly {
clear
use "CleanedData_House.dta"
drop if state == "CT"
keep cong billid icpsr dem extreme withparty afterprimary pday rcday
keep if cong == `k'
egen pdayid = group(pday)
sum pdayid
local npdays = r(max) - 1
local maxnpdays = max(`npdays', `maxnpdays')
forvalues i = 1/`npdays' {
preserve
sum pday if pdayid == `i'
keep if rcday <= r(mean) + 30 & rcday >= r(mean) - 30
egen meanafterprimary = mean(afterprimary), by(icpsr)
keep if pdayid == `i' | meanafterprimary == 0
keep icpsr billid dem extreme withparty afterprimary
g dataset = `i'
g cong = `k' 
save "dataset_`i'.dta", replace
restore
}
clear
use "dataset_1.dta"
forvalues i = 2/`npdays' {
append using "dataset_`i'.dta"
}
save "dataset_cong_`k'.dta", replace
}
disp `k'
}
clear
use "dataset_cong_104.dta"
forvalues k = 105/117 {
append using "dataset_cong_`k'.dta"
}
save "House_Stacked_v3.dta", replace
forvalues k = 104/117 {
erase "dataset_cong_`k'.dta"
}
forvalues i = 1/`maxnpdays' {
erase "dataset_`i'.dta"
}
*Generate data set for Senate
clear
local maxnpdays = 0
forvalues k = 104/117 {
quietly {
clear
use "CleanedData_Senate.dta"
keep cong billid icpsr dem extreme withparty afterprimary pday rcday
keep if cong == `k'
egen pdayid = group(pday)
sum pdayid
local npdays = r(max) - 1
local maxnpdays = max(`npdays', `maxnpdays')
forvalues i = 1/`npdays' {
preserve
sum pday if pdayid == `i'
keep if rcday <= r(mean) + 30 & rcday >= r(mean) - 30
egen meanafterprimary = mean(afterprimary), by(icpsr)
keep if pdayid == `i' | meanafterprimary == 0
keep icpsr billid dem extreme withparty afterprimary
g dataset = `i'
g cong = `k' 
save "dataset_`i'.dta", replace
restore
}
clear
use "dataset_1.dta"
forvalues i = 2/`npdays' {
append using "dataset_`i'.dta"
}
save "dataset_cong_`k'.dta", replace
}
disp `k'
}
clear
use "dataset_cong_104.dta"
forvalues k = 105/117 {
append using "dataset_cong_`k'.dta"
}
save "Senate_Stacked_v3.dta", replace
forvalues k = 104/117 {
erase "dataset_cong_`k'.dta"
}
forvalues i = 1/`maxnpdays' {
erase "dataset_`i'.dta"
}
*Regressions
clear
use "House_Stacked_v3.dta"
egen member_cong_dataset = group(icpsr dem cong dataset)
egen bill_party_dataset = group(cong billid dem dataset)
keep extreme withparty afterprimary member_cong_dataset bill_party_dataset icpsr billid
areg extreme afterprimary, a(member_cong_dataset bill_party_dataset) vce(cluster icpsr billid)
outreg2 using TableA5_Panel3, replace dec(4) alpha(.01, .05) symbol(**, *)
areg withparty afterprimary, a(member_cong_dataset bill_party_dataset) vce(cluster icpsr billid)
outreg2 using TableA5_Panel3, append dec(4) alpha(.01, .05) symbol(**, *)
clear
use "Senate_Stacked_v3.dta"
egen member_cong_dataset = group(icpsr dem cong dataset)
egen bill_party_dataset = group(cong billid dem dataset)
keep extreme withparty afterprimary member_cong_dataset bill_party_dataset icpsr billid
areg extreme afterprimary, a(member_cong_dataset bill_party_dataset) vce(cluster icpsr billid)
outreg2 using TableA5_Panel3, append dec(4) alpha(.01, .05) symbol(**, *)
areg withparty afterprimary, a(member_cong_dataset bill_party_dataset) vce(cluster icpsr billid)
outreg2 using TableA5_Panel3, append dec(4) alpha(.01, .05) symbol(**, *)

**Panel 4**
*Generate data set for House
clear
local maxnpdays = 0
forvalues k = 104/117 {
quietly {
clear
use "CleanedData_House.dta"
drop if state == "CT"
keep cong billid icpsr dem extreme withparty afterprimary pday rcday
keep if cong == `k'
egen pdayid = group(pday)
sum pdayid
local npdays = r(max) - 1
local maxnpdays = max(`npdays', `maxnpdays')
forvalues i = 2/`npdays' {
preserve
sum pday if pdayid == `i'
keep if rcday <= r(mean) + 30 & rcday >= r(mean) - 30
egen meanafterprimary = mean(afterprimary), by(icpsr)
keep if pdayid == `i' | meanafterprimary == 1
keep icpsr billid dem extreme withparty afterprimary
g dataset = `i'
g cong = `k' 
save "dataset_`i'.dta", replace
restore
}
clear
use "dataset_2.dta"
forvalues i = 3/`npdays' {
append using "dataset_`i'.dta"
}
save "dataset_cong_`k'.dta", replace
}
disp `k'
}
clear
use "dataset_cong_104.dta"
forvalues k = 105/117 {
append using "dataset_cong_`k'.dta"
}
save "House_Stacked_v4.dta", replace
forvalues k = 104/117 {
erase "dataset_cong_`k'.dta"
}
forvalues i = 2/`maxnpdays' {
erase "dataset_`i'.dta"
}
*Generate data set for Senate
clear
local maxnpdays = 0
forvalues k = 104/117 {
quietly {
clear
use "CleanedData_Senate.dta"
keep cong billid icpsr dem extreme withparty afterprimary pday rcday
keep if cong == `k'
egen pdayid = group(pday)
sum pdayid
local npdays = r(max) - 1
local maxnpdays = max(`npdays', `maxnpdays')
forvalues i = 2/`npdays' {
preserve
sum pday if pdayid == `i'
keep if rcday <= r(mean) + 30 & rcday >= r(mean) - 30
egen meanafterprimary = mean(afterprimary), by(icpsr)
keep if pdayid == `i' | meanafterprimary == 1
keep icpsr billid dem extreme withparty afterprimary
g dataset = `i'
g cong = `k' 
save "dataset_`i'.dta", replace
restore
}
clear
use "dataset_2.dta"
forvalues i = 3/`npdays' {
append using "dataset_`i'.dta"
}
save "dataset_cong_`k'.dta", replace
}
disp `k'
}
clear
use "dataset_cong_104.dta"
forvalues k = 105/117 {
append using "dataset_cong_`k'.dta"
}
save "Senate_Stacked_v4.dta", replace
forvalues k = 104/117 {
erase "dataset_cong_`k'.dta"
}
forvalues i = 2/`maxnpdays' {
erase "dataset_`i'.dta"
}
*Regressions
clear
use "House_Stacked_v4.dta"
egen member_cong_dataset = group(icpsr dem cong dataset)
egen bill_party_dataset = group(cong billid dem dataset)
keep extreme withparty afterprimary member_cong_dataset bill_party_dataset icpsr billid
areg extreme afterprimary, a(member_cong_dataset bill_party_dataset) vce(cluster icpsr billid)
outreg2 using TableA5_Panel4, replace dec(4) alpha(.01, .05) symbol(**, *)
areg withparty afterprimary, a(member_cong_dataset bill_party_dataset) vce(cluster icpsr billid)
outreg2 using TableA5_Panel4, append dec(4) alpha(.01, .05) symbol(**, *)
clear
use "Senate_Stacked_v4.dta"
egen member_cong_dataset = group(icpsr dem cong dataset)
egen bill_party_dataset = group(cong billid dem dataset)
keep extreme withparty afterprimary member_cong_dataset bill_party_dataset icpsr billid
areg extreme afterprimary, a(member_cong_dataset bill_party_dataset) vce(cluster icpsr billid)
outreg2 using TableA5_Panel4, append dec(4) alpha(.01, .05) symbol(**, *)
areg withparty afterprimary, a(member_cong_dataset bill_party_dataset) vce(cluster icpsr billid)
outreg2 using TableA5_Panel4, append dec(4) alpha(.01, .05) symbol(**, *)


***Figures A3, A4, and A5 and Table A6***
*House
clear
use "CleanedData_House.dta"
drop if state == "CT"
drop if retiring == 1
collapse congress icpsr eyear pres dem retiring born died, by(member_cong)
sort icpsr congress
g notrunningnext = icpsr != icpsr[_n+1]
tab eyear notrunningnext
drop if eyear == 2022
replace notrunningnext = 1 if died <= eyear + 2
g age = eyear - born
g oldest = age >= 80
g age_oldest = (age - 80)*oldest
g age2 = age^2
	preserve
	lpoly notrunningnext age, noscatter ci gen(p0) at(age) nograph
	reg notrunningnext age
	predict p1
	logit notrunningnext age age_oldest
	predict p2
	reg notrunningnext age age2
	predict p3
	drop pres
	g count = 1
	collapse (mean) notrunningnext p* (sum) count, by(age)
	sort age
	graph twoway (line p1 p3 p2 p0 age) (scatter notrunningnext age [w = count])
	gr_edit .legend.plotregion1.draw_view.setstyle, style(no)
	gr_edit .xaxis1.reset_rule 30 90 10 , tickset(major) ruletype(range) 
	gr_edit .xaxis1.title.text = {}
	gr_edit .xaxis1.title.text.Arrpush Age
	gr_edit .yaxis1.title.text = {}
	gr_edit .yaxis1.title.text.Arrpush Pr(Not Running Again)
	gr_edit .plotregion1.plot5.style.editstyle marker(size(vsmall)) editcopy
	gr_edit .plotregion1.plot5.style.editstyle marker(fillcolor(white)) editcopy
	gr_edit .plotregion1.plot5.style.editstyle marker(fillcolor(%0)) editcopy
	gr_edit .plotregion1.plot5.style.editstyle marker(linestyle(color(black))) editcopy
	gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
	gr_edit .xaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
	gr_edit .plotregion1.plot1.style.editstyle line(width(thick)) editcopy
	gr_edit .plotregion1.plot2.style.editstyle line(width(thick)) editcopy
	gr_edit .plotregion1.plot3.style.editstyle line(width(thick)) editcopy
	gr_edit .plotregion1.plot4.style.editstyle line(width(thick)) editcopy
	gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
	graph save "PRun_Age_House.gph", replace
	restore
egen meanpres = mean(pres), by(cong)
replace pres = pres - meanpres
drop meanpres
g ownpartypres = pres
replace ownpartypres = pres*-1 if dem == 1
	preserve
	g ownpartypresbin = round(ownpartypres*100)/100
	logit notrunningnext ownpartypres
	g p_logit = exp(_b[_cons] + _b[ownpartypres]*ownpartypresbin)/(exp(_b[_cons] + _b[ownpartypres]*ownpartypresbin) + 1)
	lpoly notrunningnext ownpartypres, nograph gen(p_lpoly) at(ownpartypresbin)
	g count = 1
	collapse (mean) p_logit p_lpoly notrunningnext (sum) count, by(ownpartypresbin)
	graph twoway (line p_logit ownpartypresbin) (scatter notrunningnext ownpartypresbin [w = count])
	gr_edit .legend.plotregion1.draw_view.setstyle, style(no)
	gr_edit .xaxis1.title.text = {}
	gr_edit .xaxis1.title.text.Arrpush Own Party Favorability
	gr_edit .yaxis1.title.text = {}
	gr_edit .yaxis1.title.text.Arrpush Pr(Not Running Again)
	gr_edit .plotregion1.plot2.style.editstyle marker(size(vsmall)) editcopy
	gr_edit .plotregion1.plot2.style.editstyle marker(fillcolor(white)) editcopy
	gr_edit .plotregion1.plot2.style.editstyle marker(fillcolor(%0)) editcopy
	gr_edit .plotregion1.plot2.style.editstyle marker(linestyle(color(black))) editcopy
	gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
	gr_edit .xaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
	gr_edit .plotregion1.plot1.style.editstyle line(width(thick)) editcopy
	gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
	graph save "PRun_OwnParty_House.gph", replace
	restore
logit notrunningnext age age_oldest ownpartypres
outreg2 using TableA6, replace dec(3) alpha(.01, .05) symbol(**, *)
predict p_notrunningnext
kdensity p_notrunningnext, at(p_notrunningnext)
gr_edit .legend.plotregion1.draw_view.setstyle, style(no)
gr_edit .xaxis1.reset_rule .1 .6 .1 , tickset(major) ruletype(range) 
gr_edit .xaxis1.title.text = {}
gr_edit .xaxis1.title.text.Arrpush Pr(Not Running Again)
gr_edit .yaxis1.title.text = {}
gr_edit .yaxis1.title.text.Arrpush Density
gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .xaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .plotregion1.plot1.style.editstyle line(color(black) width(thick)) editcopy
gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
gr_edit .title.text = {}
gr_edit .title.text.Arrpush U.S. House
gr_edit .note.draw_view.setstyle, style(no)
graph save "PRun_Density_House.gph", replace
keep congress icpsr p_notrunningnext
save "p_notrunningnext_house.dta", replace
*Senate
clear
use "CleanedData_Senate.dta"
drop if retiring == 1
replace pres = 1 - pres
collapse congress icpsr eyear pres dem retiring born died, by(member_cong)
sort icpsr congress
g notrunningnext = icpsr != icpsr[_n+3]
tab eyear notrunningnext
drop if eyear >= 2018
replace notrunningnext = 1 if died <= eyear + 6
g age = eyear - born
g oldest = age >= 80
g age_oldest = (age - 80)*oldest
g age2 = age^2
	preserve
	lpoly notrunningnext age, noscatter ci gen(p0) at(age) nograph
	reg notrunningnext age
	predict p1
	logit notrunningnext age age_oldest
	predict p2
	reg notrunningnext age age2
	predict p3
	g count = 1
	collapse (mean) notrunningnext p* (sum) count, by(age)
	sort age
	graph twoway (line p1 p3 p2 p0 age) (scatter notrunningnext age [w = count])
	gr_edit .legend.plotregion1.draw_view.setstyle, style(no)
	gr_edit .xaxis1.reset_rule 40 90 10 , tickset(major) ruletype(range) 
	gr_edit .xaxis1.title.text = {}
	gr_edit .xaxis1.title.text.Arrpush Age
	gr_edit .yaxis1.title.text = {}
	gr_edit .yaxis1.title.text.Arrpush Pr(Not Running Again)
	gr_edit .plotregion1.plot5.style.editstyle marker(size(vsmall)) editcopy
	gr_edit .plotregion1.plot5.style.editstyle marker(fillcolor(white)) editcopy
	gr_edit .plotregion1.plot5.style.editstyle marker(fillcolor(%0)) editcopy
	gr_edit .plotregion1.plot5.style.editstyle marker(linestyle(color(black))) editcopy
	gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
	gr_edit .xaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
	gr_edit .plotregion1.plot1.style.editstyle line(width(thick)) editcopy
	gr_edit .plotregion1.plot2.style.editstyle line(width(thick)) editcopy
	gr_edit .plotregion1.plot3.style.editstyle line(width(thick)) editcopy
	gr_edit .plotregion1.plot4.style.editstyle line(width(thick)) editcopy
	gr_edit .plotregion1.AddTextBox added_text editor .61 86
	gr_edit .plotregion1.added_text_new = 1
	gr_edit .plotregion1.added_text_rec = 1
	gr_edit .plotregion1.added_text[1].style.editstyle  angle(default) size( sztype(relative) val(3.4722) allow_pct(1)) color(black) horizontal(left) vertical(middle) margin( gleft( sztype(relative) val(0) allow_pct(1)) gright( sztype(relative) val(0) allow_pct(1)) gtop( sztype(relative) val(0) allow_pct(1)) gbottom( sztype(relative) val(0) allow_pct(1))) linegap( sztype(relative) val(0) allow_pct(1)) drawbox(no) boxmargin( gleft( sztype(relative) val(0) allow_pct(1)) gright( sztype(relative) val(0) allow_pct(1)) gtop( sztype(relative) val(0) allow_pct(1)) gbottom( sztype(relative) val(0) allow_pct(1))) fillcolor(bluishgray) linestyle( width( sztype(relative) val(.2) allow_pct(1)) color(black) pattern(solid) align(inside)) box_alignment(east) editcopy
	gr_edit .plotregion1.added_text[1].style.editstyle size(small) editcopy
	gr_edit .plotregion1.added_text[1].text = {}
	gr_edit .plotregion1.added_text[1].text.Arrpush Linear
	gr_edit .plotregion1.added_text[1].style.editstyle color(stc1) editcopy
	gr_edit .plotregion1.AddTextBox added_text editor .8 87.5
	gr_edit .plotregion1.added_text_new = 2
	gr_edit .plotregion1.added_text_rec = 2
	gr_edit .plotregion1.added_text[2].style.editstyle  angle(default) size( sztype(relative) val(3.4722) allow_pct(1)) color(black) horizontal(left) vertical(middle) margin( gleft( sztype(relative) val(0) allow_pct(1)) gright( sztype(relative) val(0) allow_pct(1)) gtop( sztype(relative) val(0) allow_pct(1)) gbottom( sztype(relative) val(0) allow_pct(1))) linegap( sztype(relative) val(0) allow_pct(1)) drawbox(no) boxmargin( gleft( sztype(relative) val(0) allow_pct(1)) gright( sztype(relative) val(0) allow_pct(1)) gtop( sztype(relative) val(0) allow_pct(1)) gbottom( sztype(relative) val(0) allow_pct(1))) fillcolor(bluishgray) linestyle( width( sztype(relative) val(.2) allow_pct(1)) color(black) pattern(solid) align(inside)) box_alignment(east) editcopy
	gr_edit .plotregion1.added_text[2].style.editstyle size(small) editcopy
	gr_edit .plotregion1.added_text[2].text = {}
	gr_edit .plotregion1.added_text[2].text.Arrpush Quadratic
	gr_edit .plotregion1.added_text[2].style.editstyle color(stc2) editcopy
	gr_edit .plotregion1.AddTextBox added_text editor .9 78.5
	gr_edit .plotregion1.added_text_new = 3
	gr_edit .plotregion1.added_text_rec = 3
	gr_edit .plotregion1.added_text[3].style.editstyle  angle(default) size( sztype(relative) val(3.4722) allow_pct(1)) color(black) horizontal(left) vertical(middle) margin( gleft( sztype(relative) val(0) allow_pct(1)) gright( sztype(relative) val(0) allow_pct(1)) gtop( sztype(relative) val(0) allow_pct(1)) gbottom( sztype(relative) val(0) allow_pct(1))) linegap( sztype(relative) val(0) allow_pct(1)) drawbox(no) boxmargin( gleft( sztype(relative) val(0) allow_pct(1)) gright( sztype(relative) val(0) allow_pct(1)) gtop( sztype(relative) val(0) allow_pct(1)) gbottom( sztype(relative) val(0) allow_pct(1))) fillcolor(bluishgray) linestyle( width( sztype(relative) val(.2) allow_pct(1)) color(black) pattern(solid) align(inside)) box_alignment(east) editcopy
	gr_edit .plotregion1.added_text[3].style.editstyle size(small) editcopy
	gr_edit .plotregion1.added_text[3].text = {}
	gr_edit .plotregion1.added_text[3].text.Arrpush Logit Spline
	gr_edit .plotregion1.added_text[3].style.editstyle color(stc3) editcopy
	gr_edit .plotregion1.AddTextBox added_text editor .44 67.7
	gr_edit .plotregion1.added_text_new = 4
	gr_edit .plotregion1.added_text_rec = 4
	gr_edit .plotregion1.added_text[4].style.editstyle  angle(default) size( sztype(relative) val(3.4722) allow_pct(1)) color(black) horizontal(left) vertical(middle) margin( gleft( sztype(relative) val(0) allow_pct(1)) gright( sztype(relative) val(0) allow_pct(1)) gtop( sztype(relative) val(0) allow_pct(1)) gbottom( sztype(relative) val(0) allow_pct(1))) linegap( sztype(relative) val(0) allow_pct(1)) drawbox(no) boxmargin( gleft( sztype(relative) val(0) allow_pct(1)) gright( sztype(relative) val(0) allow_pct(1)) gtop( sztype(relative) val(0) allow_pct(1)) gbottom( sztype(relative) val(0) allow_pct(1))) fillcolor(bluishgray) linestyle( width( sztype(relative) val(.2) allow_pct(1)) color(black) pattern(solid) align(inside)) box_alignment(east) editcopy
	gr_edit .plotregion1.added_text[4].style.editstyle size(small) editcopy
	gr_edit .plotregion1.added_text[4].text = {}
	gr_edit .plotregion1.added_text[4].text.Arrpush Local Polynomial
	gr_edit .plotregion1.added_text[4].style.editstyle color(stc4) editcopy
	gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
	graph save "PRun_Age_Senate.gph", replace
	restore
egen meanpres = mean(pres), by(cong)
replace pres = pres - meanpres
drop meanpres
g ownpartypres = pres
replace ownpartypres = pres*-1 if dem == 1
	preserve
	g ownpartypresbin = round(ownpartypres*100)/100
	logit notrunningnext ownpartypres
	g p_logit = exp(_b[_cons] + _b[ownpartypres]*ownpartypresbin)/(exp(_b[_cons] + _b[ownpartypres]*ownpartypresbin) + 1)
	lpoly notrunningnext ownpartypres, nograph gen(p_lpoly) at(ownpartypresbin)
	g count = 1
	collapse (mean) p_logit p_lpoly notrunningnext (sum) count, by(ownpartypresbin)
	graph twoway (line p_logit ownpartypresbin) (scatter notrunningnext ownpartypresbin [w = count])
	gr_edit .legend.plotregion1.draw_view.setstyle, style(no)
	gr_edit .xaxis1.reset_rule -.1 .2 .1 , tickset(major) ruletype(range) 
	gr_edit .xaxis1.title.text = {}
	gr_edit .xaxis1.title.text.Arrpush Own Party Favorability
	gr_edit .yaxis1.title.text = {}
	gr_edit .yaxis1.title.text.Arrpush Pr(Not Running Again)
	gr_edit .plotregion1.plot2.style.editstyle marker(size(vsmall)) editcopy
	gr_edit .plotregion1.plot2.style.editstyle marker(fillcolor(white)) editcopy
	gr_edit .plotregion1.plot2.style.editstyle marker(fillcolor(%0)) editcopy
	gr_edit .plotregion1.plot2.style.editstyle marker(linestyle(color(black))) editcopy
	gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
	gr_edit .xaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
	gr_edit .plotregion1.plot1.style.editstyle line(width(thick)) editcopy
	gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
	graph save "PRun_OwnParty_Senate.gph", replace
	restore
logit notrunningnext age age_oldest ownpartypres
outreg2 using TableA6, append dec(3) alpha(.01, .05) symbol(**, *)
predict p_notrunningnext
kdensity p_notrunningnext, at(p_notrunningnext)
gr_edit .legend.plotregion1.draw_view.setstyle, style(no)
gr_edit .xaxis1.reset_rule .2 1 .2 , tickset(major) ruletype(range) 
gr_edit .xaxis1.title.text = {}
gr_edit .xaxis1.title.text.Arrpush Pr(Not Running Again)
gr_edit .yaxis1.title.text = {}
gr_edit .yaxis1.title.text.Arrpush Density
gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .xaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .plotregion1.plot1.style.editstyle line(color(black) width(thick)) editcopy
gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
gr_edit .title.text = {}
gr_edit .title.text.Arrpush U.S. Senate
gr_edit .note.draw_view.setstyle, style(no)
graph save "PRun_Density_Senate.gph", replace
keep congress icpsr p_notrunningnext
save "p_notrunningnext_senate.dta", replace
*Combined
graph combine "PRun_Age_House.gph" "PRun_Age_Senate.gph", col(1)
gr_edit .style.editstyle margin(zero) boxstyle(shadestyle(color(white)) linestyle(color(white))) declared_ysize(6) editcopy
gr_edit .plotregion1.graph1.xaxis1.title.draw_view.setstyle, style(no)
gr_edit .plotregion1.graph1.title.style.editstyle color(black) size(medlarge) editcopy
gr_edit .plotregion1.graph1.title.text = {}
gr_edit .plotregion1.graph1.title.text.Arrpush U.S. House
gr_edit .plotregion1.graph1.title.style.editstyle  editcopy
gr_edit .plotregion1.graph2.title.style.editstyle color(black) size(medlarge) editcopy
gr_edit .plotregion1.graph2.title.text = {}
gr_edit .plotregion1.graph2.title.text.Arrpush U.S. Senate
graph export "FigureA3.png", replace as(png)
graph combine "PRun_OwnParty_House.gph" "PRun_OwnParty_Senate.gph", col(1)
gr_edit .style.editstyle margin(zero) boxstyle(shadestyle(color(white)) linestyle(color(white))) declared_ysize(6) editcopy
gr_edit .plotregion1.graph1.xaxis1.title.draw_view.setstyle, style(no)
gr_edit .plotregion1.graph1.title.style.editstyle color(black) size(medlarge) editcopy
gr_edit .plotregion1.graph1.title.text = {}
gr_edit .plotregion1.graph1.title.text.Arrpush U.S. House
gr_edit .plotregion1.graph1.title.style.editstyle  editcopy
gr_edit .plotregion1.graph2.title.style.editstyle color(black) size(medlarge) editcopy
gr_edit .plotregion1.graph2.title.text = {}
gr_edit .plotregion1.graph2.title.text.Arrpush U.S. Senate
graph export "FigureA4.png", replace as(png)
graph combine "PRun_Density_House.gph" "PRun_Density_Senate.gph", col(2)
gr_edit .style.editstyle margin(zero) boxstyle(shadestyle(color(white)) linestyle(color(white))) declared_ysize(4) editcopy
gr_edit .style.editstyle margin(zero) boxstyle(shadestyle(color(white)) linestyle(color(white))) declared_xsize(8) editcopy
graph export "FigureA5.png", replace as(png)


***Figure A6***
*We will use the "p_notrunningnext" files that we generated above
*House
clear
use "CleanedData_House.dta"
drop if state == "CT"
drop if retiring == 1
drop if eyear == 2022
merge m:1 congress icpsr using "p_notrunningnext_house.dta"
drop _merge
keep extreme afterprimary p_notrunningnext member_cong bill_party icpsr billid
postfile P_Notrunning cutoff coef upper lower pval using "P_Notrunning.dta", replace
forvalues i = .05(.025).651 {
g lowprob = p_notrunningnext < `i'
g afterprimary_lowprob = afterprimary*lowprob
areg extreme afterprimary afterprimary_lowprob, a(member_cong bill_party) vce(cluster icpsr billid)
post P_Notrunning (`i') (_b[afterprimary]) (_b[afterprimary] + _se[afterprimary]*invttail(e(df_r),.025)) (_b[afterprimary] - _se[afterprimary]*invttail(e(df_r),.025)) (2*ttail(e(df_r),abs(_b[afterprimary]/_se[afterprimary]))) 
drop lowprob afterprimary_lowprob
}
postclose P_Notrunning
clear
use "P_Notrunning.dta"
graph twoway line coef upper lower cutoff, yline(0)
gr_edit .plotregion1._xylines[1].style.editstyle linestyle(color(gs12) width(medthin) pattern(solid)) editcopy
gr_edit .legend.draw_view.setstyle, style(no)
gr_edit .xaxis1.title.text = {}
gr_edit .xaxis1.title.text.Arrpush Cutoff for Probability of Not Running Next Time
gr_edit .yaxis1.title.text = {}
gr_edit .yaxis1.title.text.Arrpush Effect of Primary Date
gr_edit .xaxis1.reset_rule .05 .65 .15, tickset(major) ruletype(range)  
gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .xaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
gr_edit .plotregion1.plot2.style.editstyle line(color(black) width(medthin) pattern(dash)) editcopy
gr_edit .plotregion1.plot3.style.editstyle line(color(black) width(medthin) pattern(dash)) editcopy
gr_edit .plotregion1.plot1.style.editstyle line(color(black) width(thick)) editcopy
gr_edit .style.editstyle declared_ysize(4) editcopy
gr_edit .style.editstyle declared_xsize(6) editcopy
graph save "P_Notrunning_House.gph", replace
*Senate
clear
use "CleanedData_Senate.dta"
drop if retiring == 1
drop if eyear >= 2018
merge m:1 congress icpsr using "p_notrunningnext_senate.dta"
drop _merge
keep extreme afterprimary p_notrunningnext member_cong bill_party icpsr billid
postfile P_Notrunning cutoff coef upper lower pval using "P_Notrunning.dta", replace
forvalues i = .15(.01).9 {
g lowprob = p_notrunningnext < `i'
g afterprimary_lowprob = afterprimary*lowprob
areg extreme afterprimary afterprimary_lowprob, a(member_cong bill_party) vce(cluster icpsr billid)
post P_Notrunning (`i') (_b[afterprimary]) (_b[afterprimary] + _se[afterprimary]*invttail(e(df_r),.025)) (_b[afterprimary] - _se[afterprimary]*invttail(e(df_r),.025)) (2*ttail(e(df_r),abs(_b[afterprimary]/_se[afterprimary]))) 
drop lowprob afterprimary_lowprob
}
postclose P_Notrunning
clear
use "P_Notrunning.dta"
graph twoway line coef upper lower cutoff, yline(0)
gr_edit .plotregion1._xylines[1].style.editstyle linestyle(color(gs12) width(medthin) pattern(solid)) editcopy
gr_edit .legend.draw_view.setstyle, style(no)
gr_edit .xaxis1.title.text = {}
gr_edit .xaxis1.title.text.Arrpush Cutoff for Probability of Not Running Next Time
gr_edit .yaxis1.title.text = {}
gr_edit .yaxis1.title.text.Arrpush Effect of Primary Date
gr_edit .xaxis1.reset_rule .15 .9 .15, tickset(major) ruletype(range) 
gr_edit .yaxis1.reset_rule -.06 0 .02 , tickset(major) ruletype(range) 
gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .xaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
gr_edit .plotregion1.plot2.style.editstyle line(color(black) width(medthin) pattern(dash)) editcopy
gr_edit .plotregion1.plot3.style.editstyle line(color(black) width(medthin) pattern(dash)) editcopy
gr_edit .plotregion1.plot1.style.editstyle line(color(black) width(thick)) editcopy
gr_edit .style.editstyle declared_ysize(4) editcopy
gr_edit .style.editstyle declared_xsize(6) editcopy
graph save "P_Notrunning_Senate.gph", replace
*Combined
graph combine "P_Notrunning_House.gph" "P_Notrunning_Senate.gph", col(1)
gr_edit .style.editstyle margin(zero) boxstyle(shadestyle(color(white)) linestyle(color(white))) declared_ysize(6) editcopy
gr_edit .plotregion1.graph1.xaxis1.title.draw_view.setstyle, style(no)
gr_edit .plotregion1.graph1.title.style.editstyle color(black) size(medlarge) editcopy
gr_edit .plotregion1.graph1.title.text = {}
gr_edit .plotregion1.graph1.title.text.Arrpush U.S. House
gr_edit .plotregion1.graph1.title.style.editstyle  editcopy
gr_edit .plotregion1.graph2.title.style.editstyle color(black) size(medlarge) editcopy
gr_edit .plotregion1.graph2.title.text = {}
gr_edit .plotregion1.graph2.title.text.Arrpush U.S. Senate
graph export "FigureA6.png", replace as(png)
